#region File Description
//  DomoBCI 
//  License: GNU/GPL
//  Developer: Federico Leonardo Motta
//  File description: This component display the dialog for the creation of a new user.
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using DomoBCI.Core.GameManager;
using DomoBCI.gui;
using SharedContent;
#endregion

namespace DomoBCI.Core.Screens
{
    public delegate void DialogAction();

    public class CreateUser : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public event DialogAction OnCancel;
        public event DialogAction OnConfirm;

        SpriteBatch spriteBatch;
        float screenscale;
        IInputManager InputManagerService;
        IComunicationManager cm;
        Rectangle area;
        UIcontainer DialogCont;
        UIlabel label1,label2,label3;
        UIbutton ok,cancel;
        UIentry entry;
        IAsyncResult res;
        StorageDevice device;

        public CreateUser(Game game, IInputManager Input_Manager_Service, float Screenscale, Rectangle Area, SpriteBatch sprite_Batch)
            : base(game)
        {
            res = StorageDevice.BeginShowSelector(0, null, null);
            device = StorageDevice.EndShowSelector(res);
            screenscale = Screenscale;
            InputManagerService = Input_Manager_Service;
            area = Area;
            spriteBatch = sprite_Batch;
            InputManagerService.ClearState();
            InputManagerService.EnterPressed += new PressedEventHandler(EnterPressed);
            InputManagerService.EnterReleased += new PressedEventHandler(EnterReleased);
            InputManagerService.TabPressed += new PressedEventHandler(TabPressed);
            InputManagerService.RightPressed += new PressedEventHandler(RightPressed);
            InputManagerService.LeftPressed += new PressedEventHandler(LeftPressed);
            cm = (IComunicationManager)Game.Services.GetService(typeof(IComunicationManager));
        }

        public override void Initialize()
        {
            float x_pos = (float)Game.GraphicsDevice.Viewport.Width / 4f;
            float y_pos = (float)Game.GraphicsDevice.Viewport.Height / 4f;
            DialogCont = new UIcontainer("DialCont", new Vector2(1.05f*x_pos, 1.32f*y_pos), "dialog_login", Game);
            entry = new UIentry(new Vector2(1.54f * x_pos, 2.4f * y_pos), "TNR", "GUI/entry/entry", "GUI/entry/entry_sel", Game);
            entry.Select = true;
            label1 = new UIlabel("NewUserLabel","Inserire il nome utente\nnel riquadro seguente","TNR",Game);
            label1.Position = new Vector2(1.54f * x_pos, y_pos * 1.7f);
            string str = "(NB: Il nome deve contenere solamente\ncaratteri alfa numerici)";
            label2 = new UIlabel("NewUserLabel", str, "Arial", Game);
            label2.Position = new Vector2(1.54f * x_pos, y_pos * 2.1f);
            ok = new UIbutton("Ok", new Vector2(1.54f * x_pos, y_pos*2.75f),
                     "GUI/ok/ok", "GUI/ok/ok_pres",
                     "GUI/ok/ok_sel", "GUI/ok/ok_inact", Game);
            cancel = new UIbutton("cancel", new Vector2(2.04f * x_pos, y_pos * 2.75f),
                     "GUI/cancel/cancel", "GUI/cancel/cancel_pres",
                     "GUI/cancel/cancel_sel", "GUI/cancel/cancel_inact", Game);
            label3 = new UIlabel("EntryLabel", "|", "Arial", Game);
            label3.Position = new Vector2(1.6f * x_pos, y_pos * 2.4f);
            entry.Text = "";
            base.Initialize();
        }

        private void TabPressed()
        {
            if (entry.Select)
            {
                entry.Select = false;
                ok.Select = true;
            }
            else if (ok.Select)
            {
                ok.Select = false;
                cancel.Select = true;
            }
            else if (cancel.Select)
            {
                cancel.Select = false;
                entry.Select = true;
            }
        }

        private void EnterReleased()
        {
            if (ok.Select)
            {
                ok.IsPressed = false;
            }
            else if (cancel.Select)
            {
                cancel.IsPressed = false;
            }
        }

        private void EnterPressed()
        {
            if (ok.Select)
            {
                ok.IsPressed = true;
                if(entry.Text != "" || entry.Text != null){
                    User usr = new User();
                    usr.SetUser(entry.Text);
                    FileManager fm = new FileManager();
                    fm.DoSave(device,usr);

                    cm.HIMCreateUser(usr);
                    usr.Nome = "";
                    usr.Id = 0;
                    fm.DoDelete(device, usr);
                    }
                if (OnConfirm != null) OnConfirm();
                cancel.Select = false;
                entry.Select = false;
                entry.Text = "";
            }
            else if (cancel.Select)
            {
                cancel.IsPressed = true;
                if (OnCancel != null) OnCancel();
                cancel.Select = false;
                entry.Select = false;
            }
            else if (entry.Select)
            {
                entry.IsPressed = true;
            }
        }

        private void RightPressed()
        {}

        private void LeftPressed()
        {}

        public override void Update(GameTime gameTime)
        {
            label3.Text = entry.Text;
            entry.UpdateButton(gameTime);
            ok.UpdateButton(gameTime);
            cancel.UpdateButton(gameTime);
            base.Update(gameTime);
        }

        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(CreateUser));
            base.Dispose(disposing);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(DialogCont.Skin, DialogCont.Position, null, Color.White, 0f, Vector2.Zero, screenscale, SpriteEffects.None, 0f);
            spriteBatch.Draw(entry.CurrentSkin, entry.Position, null, Color.White, 0f, Vector2.Zero, screenscale, SpriteEffects.None, 0f);
            spriteBatch.DrawString(label1.Font, label1.Text, label1.Position, Color.White,0.0f, new Vector2(0, 0), screenscale*1.2f, SpriteEffects.None, 0);
            spriteBatch.DrawString(label2.Font, label2.Text, label2.Position, Color.White, 0.0f, new Vector2(0, 0), screenscale * 0.6f, SpriteEffects.None, 0);
            spriteBatch.DrawString(label3.Font, label3.Text, label3.Position*1.01f, Color.White, 0.0f, new Vector2(0, 0), screenscale * 1.2f, SpriteEffects.None, 0);
            spriteBatch.Draw(ok.CurrentSkin, ok.Position, null, Color.White, 0f, Vector2.Zero, screenscale, SpriteEffects.None, 0f);
            spriteBatch.Draw(cancel.CurrentSkin, cancel.Position, null, Color.White, 0f, Vector2.Zero, screenscale, SpriteEffects.None, 0f);
            spriteBatch.End();  
            base.Draw(gameTime);
        }
    }
}
